
import os
from .tool.dawate_chat import dawate_streaming

# 设置你的OpenAI API密钥
os.environ["OPENAI_API_KEY"] = "your-api-key-here"


def get_chat_response(conversation_id):
    from .models import Message
    # 获取对话历史
    messages = Message.objects.filter(conversation_id=conversation_id).order_by('timestamp')

    # 构建上下文
    context = [{"role": msg.role, "content": msg.content} for msg in messages]

    # 调用OpenAI API (示例使用gpt-3.5-turbo)
    response = dawate_streaming(context)

    return response.strip()